import json
import pyecharts.options as opts
from pyecharts.charts import Line

# 读取文件原始数据
with open("美国疫情数据.txt", mode='r', encoding='utf-8') as file:
    american_data = file.read()[26:-2]
with open("日本疫情数据.txt", mode='r', encoding='utf-8') as file:
    japan_data = file.read()[26:-3]
with open("印度疫情数据.txt", mode='r', encoding='utf-8') as file:
    india_data = file.read()[26:-3]
# 将数据转换成python能识别的数据类型
american_data = json.loads(american_data)
japan_data = json.loads(japan_data)
india_data = json.loads(india_data)
date = american_data['data'][0]['trend']['updateDate'][0:30]

american_data = american_data['data'][0]['trend']['list'][0]['data'][0:30]
japan_data = japan_data['data'][0]['trend']['list'][0]['data'][0:30]
india_data = india_data['data'][0]['trend']['list'][0]['data'][0:30]
# 将数据处理，生成可视化页面

c = (
    Line()
    .add_xaxis(date)
    .add_yaxis("美国", american_data)
    .add_yaxis("日本", japan_data)
    .add_yaxis("印度", india_data)
    .set_global_opts(
        title_opts=opts.TitleOpts(title="2020年新冠疫情"),
        xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=45))
    )
    .render("line_base.html")
)
